﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TIS.DAO;
using System.Data;

namespace TIS.Administrator
{
    public partial class ManagerPartner : System.Web.UI.Page
    {
        PartnerDAO _partnerDao = new PartnerDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
           
                loadGridView();
                loadForm();
            }
        }
        
        public void loadGridView()
        {
            gridViewPartner.DataSource = _partnerDao.GetPartnerAll();
            gridViewPartner.DataBind();
        }

        public void loadForm()
        {
            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"].ToString();
                DataTable dt = _partnerDao.GetPartnerById(int.Parse(id));
                foreach (DataRow row in dt.Rows)
                {
                    txtPosition.Text = row["PartnerPosition"].ToString();
                    txtURL.Text = row["PartnerLink"].ToString();
                    txtName.Text = row["PartnerName"].ToString();
                    if (row["PartnerStatus"].ToString().Equals("1"))
                    {
                        rdoStatusDisplay.Checked = true;
                        rdoStatusHidden.Checked = false;
                    }
                    else
                    {
                        rdoStatusDisplay.Checked = false;
                        rdoStatusHidden.Checked = true;
                    }
                }


            }

        }

        protected void lbtnEdit_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("ManagerPartner.aspx?id=" + e.CommandArgument.ToString());
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            _partnerDao.DeletePartner(int.Parse(e.CommandArgument.ToString()));
            Response.Redirect("ManagerPartner.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string imgSmall = "";
            int status;
            if (rdoStatusDisplay.Checked)
            {
                status = 1;
            }
            else
            {
                status = 0;
            }
            if (Request.QueryString["id"] == null)
            {
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }

                _partnerDao.InsertPartner(txtName.Text, txtURL.Text, imgSmall,int.Parse(txtPosition.Text), status);

                Response.Redirect("ManagerPartner.aspx");
            }
            else
            {
                string id = Request.QueryString["id"].ToString();
                if (fileUpload.FileName != "")
                {
                    imgSmall = "/Upload/Image/" + fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Upload/Image/") + fileUpload.FileName);
                }
                _partnerDao.UpdatePartner(int.Parse(id), txtName.Text, txtURL.Text, imgSmall, int.Parse(txtPosition.Text), status);
                Response.Redirect("ManagerPartner.aspx");
            }
        }

    }
}